package controller

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

// TestCookie 设置cookie
func TestCookie(context *gin.Context) {
	// 获取cookie
	cookie, err := context.Cookie("testCookie")
	if err != nil && err.Error() == "http: named cookie not present" {
		cookie = "not set"
		log.Println("cookie key = testCookie , value = testCookieValue")
		// 给客户端设置cookie
		// maxAge int, 单位为秒
		// path,cookie所在目录
		// domain string,域名
		// secure 是否智能通过https访问
		// httpOnly bool  是否允许别人通过js获取自己的cookie
		context.SetCookie("testCookie", "testCookieValue", 60,
			"/", "", false, true)
	} else {
		log.Printf("cookie exist, cookie key = testCookie , value = %s", cookie)
	}

	context.JSON(http.StatusOK, gin.H{
		"cookie": cookie,
	})

}
